import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Headpage from '../views/Headpage.vue'
import Teacherlist from '../views/Teacherlist.vue'
import Addteacher from '../views/Addteacher.vue'
import Addqtry from '../views/Addqtry.vue'
import Studentlist from '../views/Studentlist.vue'
import Studentchange from '../views/Studentchange.vue'
import Dormitorytype from '../views/Dormitorytype.vue'
import Dormitorylist from '../views/Dormitorylist.vue'
import Stardormitory from '../views/Stardormitory.vue'
import Sleepchecklist from '../views/Sleepchecklist.vue'
import Details from '../components/jiaoshiliebiao/Detailsteach.vue'
Vue.use(VueRouter)

const routes = [
    { path: "/", redirect: '/login' },
    { path: "/login", component: Login, },

    {
        path: '/home',
        component: Home,
        redirect: '/headpage',
        children: [{
                path: '/headpage',
                component: Headpage
            }, {
                path: '/teacherlist',
                component: Teacherlist
            },
            {
                path: '/addteacher',
                component: Addteacher
            },
            {
                path: '/studentlist',
                component: Studentlist
            },
            {
                path: '/studentchange',
                component: Studentchange
            },
            {
                path: '/dormitorytype',
                component: Dormitorytype
            },
            {
                path: '/dormitorylist',
                component: Dormitorylist
            },
            {
                path: '/stardormitory',
                component: Stardormitory
            },
            {
                path: '/sleepchecklist',
                component: Sleepchecklist
            },
            {
                path: '/details',
                component: Details
            },
            {
                path: '/addqtry',
                component: Addqtry
            },
        ]
    },
    // {
    //     path: '/about',
    //     name: 'About',
    //     // route level code-splitting
    //     // this generates a separate chunk (about.[hash].js) for this route
    //     // which is lazy-loaded when the route is visited.
    //     component: () =>
    //         import ( /* webpackChunkName: "about" */ '../views/About.vue')
    // }
]

const router = new VueRouter({
    routes
})

//挂在路由导航守卫
router.beforeEach((to, from, next) => {
    //to 将要访问的路径
    //from 代表从哪个路径跳转而来
    //next 是一个函数，表示放行
    //  next()  放行   next('/login') 强制跳转
    if (to.path === '/login') {
        return next()
    }
    //获取token
    const tokenStr = window.sessionStorage.getItem('token')
        //如果没有token,不可以通行
    if (!tokenStr) {
        return next('/login')
    }
    //如果有token,可以通行
    next()
})


export default router